from tkinter import Tk, Label, Entry, Button, IntVar, Frame, Listbox, Checkbutton, Radiobutton, Scrollbar, BOTH
from tkinter import ttk
from tkinter import END
import cms_scan, waf_scan, dir_scan, subdomain_scan, port_scan, ip


# ip查询
def ip_search():
    list1.delete('0', 'end')  # 删除列表框中原有的内容
    result = ip.get_ip(entry1.get())  # 先从entry1输入框获取输入的域名，再调用ip模块中的get_ip函数查询该域名对应的ip地址

    if isinstance(result[0], list):  # 判断返回结果中的元素是否为列表，如果是列表则表示查询到了对应的ip
        list1.insert(END, "域名" + " " * 26 + "ip地址" + " " * 20 + "物理地址")  # 在列表框显示每个字段的函数
        for one_result in result:  # 将每一条查询结果组合字符串，插入列表框的最后
            one_row = ''
            for r in one_result:
                one_row += r + " " * 5
            list1.insert(END, one_row)

    list1.insert(END, '-' * 52)
    list1.insert(END, '查询已完成！')
    list1.see(END)  # 指定最后一行可视
    list1.update()  # 更新列表框


# 端口扫描
def p_scan():
    list2.delete('0', 'end')

    target_host = entry2.get()  # 获取被扫描主机的ip或域名

    scan_ports = entry2_ports.get()  # 获取被扫描端口

    # 判断扫描方式
    scan_para = '-T4'  # 保存扫描参数

    if CheckVar1.get() and CheckVar2.get() and CheckVar3.get() == 0:
        match v1.get():
            case 0:
                scan_para += '-sP'
            case 1:
                scan_para += '-Pn'
            case 2:
                scan_para += '-PS'
            case 3:
                scan_para += '-PA'
        scan_ports = None

    if CheckVar2.get():  # 端口扫描
        match v2.get():
            case 0:
                scan_para += '-sT'
            case 1:
                scan_para += '-sS'

    if CheckVar3.get():  # 操作系统检测
        scan_para += ' -O'

    print(scan_para)
    results = port_scan.check_port(target_host, scan_ports, scan_para)
    for ip, host_info in results.items():
        print(ip + "在线！")
        print(host_info)

        if ip == 'ip或域名输入错误:':
            print(111111, host_info)
            list2.insert(END, host_info)
            break

        if host_info:
            list2.insert(END, ip + "在线:")
        else:
            list2.insert(END, ip + "在线！")

        for info in host_info:
            list2.insert(END, info)
        list2.insert(END, '-' * 52)

    list2.insert(END, '扫描已完成！')
    list2.see(END)
    list2.update()


# 子域名爆破
def bu3():
    list3.delete('0', 'end')
    result = subdomain_scan.domain_check(entry3.get())
    for i in result:
        list3.insert(END, i)

    list3.insert(END, '-' * 52)
    list3.insert(END, '查询已完成！')
    list3.see(END)
    list3.update()


# waf识别
def bu4():
    list4.delete('0', 'end')
    result = waf_scan.run(entry4.get())
    list4.insert(END, result)
    list4.insert(END, '-' * 52)
    list4.insert(END, '查询已完成！')
    list4.see(END)
    list4.update()


# cms识别
def bu5():
    list5.delete('0', 'end')
    result = cms_scan.run(entry5.get())
    list5.insert(END, result)
    list5.insert(END, '-' * 52)
    list5.insert(END, '查询已完成！')
    list5.see(END)
    list5.update()


# 目录扫描
def bu6():
    list6.delete('0', 'end')
    result = dir_scan.run(entry6.get())
    for i in result:
        list6.insert(END, i)

    list6.insert(END, '-' * 52)
    list6.insert(END, '查询已完成！')

    list6.see(END)
    list6.update()


if __name__ == '__main__':
    root = Tk()  # 创建主窗口
    root.title('渗透测试之系统信息收集')  # 设置主窗口的标题
    root.geometry('800x500')  # 设置主窗口的大小
    root.resizable(False, False)  # 不能改变窗口大小

    notebook = ttk.Notebook(root)  # 创建Notebook控件

    # IP查询子窗体
    frame1 = Frame(root)  # 创建ip查询子窗体
    label = Label(frame1, text='输入域名：', font=('微软雅黑', 10))  # 创建一个标签，标签名为'输入域名：'
    label.grid(row=0, column=0)  # 标签放在0行0列
    entry1 = Entry(frame1, width=35)  # 创建一个输入框控件
    entry1.insert(END, '示例:www.test.com')  # 在输入框最后插入一条数据
    entry1.grid(row=0, column=1)  # 输入框放到0行1列
    button1 = Button(frame1, text='运行', command=ip_search)  # 创建一个‘运行’按钮
    button1.grid(row=0, column=2)  # 按钮放在0行2列
    list1 = Listbox(frame1, font=('微软雅黑', 12), width=100, height=18, background='white')  # 创建一个列表框控件
    list1.grid(row=1, columnspan=40)  # 列表框放到1行，该控件所跨的列数40列

    # 端口扫描子窗体
    frame2 = Frame(root)
    label = Label(frame2, text='ip地址或域名：', font=('微软雅黑', 10))
    label.grid(row=0, columnspan=1, sticky='w')
    entry2 = Entry(frame2, width=35)
    entry2.insert(END, '示例:192.168.1.1或www.test.com')
    entry2.grid(row=0, column=1, columnspan=4, sticky='w')
    button2 = Button(frame2, text='运行', command=p_scan, background='#BEBEBE')
    button2.grid(row=0, column=5, sticky='w')

    label = Label(frame2, text="扫描端口：", font=('微软雅黑', 10))
    label.grid(row=1, columnspan=1, sticky='w')
    entry2_ports = Entry(frame2, width=70)
    entry2_ports.insert(END, "80,21,22,23,25,53,110,443,445,1433,1863,2289,3306,5631,5632,5000,8080,9090")
    entry2_ports.grid(row=1, column=1, columnspan=30, sticky='w')

    label = Label(frame2, text="扫描方式：", font=('微软雅黑', 10))
    label.grid(row=2, column=0, sticky='w')

    CheckVar1 = IntVar()
    CheckVar2 = IntVar()
    CheckVar3 = IntVar()
    CheckVar1.set(1)
    CheckVar2.set(0)
    CheckVar3.set(0)
    check1 = Checkbutton(frame2, text="主机扫描", font=('微软雅黑', 10), variable=CheckVar1, onvalue=1, offvalue=0)
    check2 = Checkbutton(frame2, text="端口扫描", font=('微软雅黑', 10), variable=CheckVar2, onvalue=1, offvalue=0)
    check3 = Checkbutton(frame2, text="操作系统检测", font=('微软雅黑', 10), variable=CheckVar3, onvalue=1,
                            offvalue=0)

    check1.grid(row=2, column=1, sticky='w')
    check2.grid(row=3, column=1, sticky='w')
    check3.grid(row=4, column=1, sticky='w')

    v1 = IntVar()
    v1.set(0)
    Radiobutton(frame2, text="-sP", variable=v1, value=0).grid(row=2, column=2, sticky='w')
    Radiobutton(frame2, text="-Pn", variable=v1, value=1).grid(row=2, column=3, sticky='w')
    Radiobutton(frame2, text="-PS", variable=v1, value=2).grid(row=2, column=4, sticky='w')
    Radiobutton(frame2, text="-PA", variable=v1, value=3).grid(row=2, column=5, sticky='w')

    v2 = IntVar()
    v2.set(0)
    Radiobutton(frame2, text="-sT", variable=v2, value=0).grid(row=3, column=2, sticky='w')
    Radiobutton(frame2, text="-sS", variable=v2, value=1).grid(row=3, column=3, sticky='w')

    list2 = Listbox(frame2, font=('微软雅黑', 12), width=100, height=18, background='white')
    list2.grid(row=5, columnspan=40)

    # 子域名爆破子窗体
    frame3 = Frame(root)
    label = Label(frame3, text='输入域名：', font=('微软雅黑', 10))
    label.grid(row=0, column=0)
    entry3 = Entry(frame3, width=35)
    entry3.insert(END, '示例:www.test.com')
    entry3.grid(row=0, column=1)
    button3 = Button(frame3, text='运行', command=bu3)
    button3.grid(row=0, column=2)
    list3 = Listbox(frame3, font=('微软雅黑', 12), width=100, height=18, background='white')
    list3.grid(row=1, columnspan=40)

    # WAF识别子窗体
    frame4 = Frame(root)
    label = Label(frame4, text='输入URL：', font=('微软雅黑', 10))
    label.grid(row=0, column=0)
    entry4 = Entry(frame4, width=35)
    entry4.insert(END, '示例:http://113.10.242.95')
    entry4.grid(row=0, column=1)
    button4 = Button(frame4, text='运行', command=bu4)
    button4.grid(row=0, column=2)
    list4 = Listbox(frame4, font=('微软雅黑', 12), width=100, height=18, background='white')
    list4.grid(row=1, columnspan=40)

    # CMS识别子窗体
    frame5 = Frame(root)
    label = Label(frame5, text='输入URL：', font=('微软雅黑', 10))
    label.grid(row=0, column=0)
    entry5 = Entry(frame5, width=35)
    entry5.insert(END, '实例:http://www.senhongjs.com.cn')
    entry5.grid(row=0, column=1)
    button5 = Button(frame5, text='运行', command=bu5)
    button5.grid(row=0, column=2)
    list5 = Listbox(frame5, font=('微软雅黑', 12), width=100, height=18, background='white')
    list5.grid(row=1, columnspan=40)

    # 目录扫描子窗体
    frame6 = Frame(root)
    label = Label(frame6, text='输入URL：', font=('微软雅黑', 10))
    label.grid(row=0, column=0)
    entry6 = Entry(frame6, width=35)
    entry6.insert(END, '实例:http://www.test.com')
    entry6.grid(row=0, column=1)
    button6 = Button(frame6, text='运行', command=bu6)
    button6.grid(row=0, column=2)
    list6 = Listbox(frame6, font=('微软雅黑', 12), width=100, height=18, background='white')
    list6.grid(row=1, columnspan=40)

    # 添加滚动条
    for frame, lst in [(frame1, list1), (frame2, list2), (frame3, list3), (frame4, list4), (frame5, list5),
                       (frame6, list6)]:
        scrollbar = Scrollbar(frame)
        scrollbar.grid(row=1, column=40, sticky='ns')
        lst.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=lst.yview)

    # 子窗体与选项卡关联
    notebook.add(frame1, text='IP查询')
    notebook.add(frame2, text='端口扫描')
    notebook.add(frame3, text='子域名爆破')
    notebook.add(frame4, text='WAF识别')
    notebook.add(frame5, text='CMS识别')
    notebook.add(frame6, text='目录扫描')
    notebook.pack(padx=10, pady=5, fill=BOTH, expand=True)

    root.mainloop()
